// pages/my/mydynamics/mydynamics.js
Page({
  data: {
    type: "1",
    delIndex: "",
    typeList: [{
      type: "1",
      name: "心情动态"
    }, {
      type: "2",
      name: "自家售卖"
    }, {
      type: "3",
      name: "出租招租"
    }, {
      type: "5",
      name: "二手市场"
    }, {
      type: "6",
      name: "旅游代买"
    }, {
      type: "7",
      name: "技能墙"
    }],
    list: [],
    page: 1,
    size: 10,
    isNoMore: true,
    isLoding: true
  },
  onLoad: function (options) {
    this.getData();
  },
  onShareAppMessage() {},
  reset() {
    this.setData({
      list: [],
      page: 1,
      isNoMore: true,
      isLoding: true
    });
    this.getData();
  },
  typeSel(e) {
    this.setData({
      type: e.currentTarget.dataset.type,
    });
    this.reset();
  },
  showDel(e) {
    let i = e.currentTarget.dataset.index;
    let key = "list[" + i + "].isDel";
    this.setData({
      delIndex: i,
      [key]: true
    });
  },
  cancelDel(e) {
    let i = this.data.delIndex;
    let key = "list[" + i + "].isDel";
    this.setData({
      [key]: false
    });
  },
  del(e) {
    getApp().globalData.vant.dialog.confirm({
        title: "确定删除吗?",
        zIndex: "500",
        message: "删除后数据将无法恢复",
        confirmButtonText: "删除",
        confirmButtonColor: "#33B868"
      })
      .then(() => {
        getApp().globalData.http.cancelSync({
          id: e.currentTarget.dataset.id
        }).then(res => {
          getApp().globalData.vant.toast.success("删除成功");
          this.reset();
        }).catch(e => {
          getApp().globalData.vant.toast.fail(e.msg);
        });
      })
      .catch(() => {
        let i = this.data.delIndex;
        let key = "list[" + i + "].isDel";
        this.setData({
          [key]: false
        });
      });
  },
  toLikedList() {
    wx.navigateTo({
      url: "/pages/my/likedlist/likedlist",
    })
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.getData();
  },
  thumbup(e) {
    getApp().globalData.http.thumbup({
      id: e.currentTarget.dataset.id
    }).then(res => {
      this.setData({
        ["list[" + e.currentTarget.dataset.index + "].isThumbsUp"]: 1,
        ["list[" + e.currentTarget.dataset.index + "].thumbsUp"]: this.data.list[e.currentTarget.dataset.index].thumbsUp / 1 + 1,
      });
    });
  },
  thumbupList(e) {
    wx.navigateTo({
      url: "/pages/my/likedlist/likedlist?id=" + e.currentTarget.dataset.id,
    });
  },
  getData() {
    let params = {
      page: this.data.page,
      size: this.data.size,
      type: this.data.type,
    };
    getApp().globalData.http.myNews(params).then(res => {
      let data_tmp = [...this.data.list, ...res.data];
      this.setData({
        list: data_tmp,
        isLoding: true
      });
      if (this.data.list.length == res.count) {
        this.setData({
          isNoMore: false
        });
      }
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    });
  },
  onReachBottom() {
    if (this.data.isNoMore) {
      this.setData({
        page: this.data.page / 1 + 1,
        isLoding: false
      });
      this.getData();
    }
  },
  previewImg(e) {
    let arr = e.currentTarget.dataset.img;
    let current = e.currentTarget.dataset.current;
    getApp().globalData.util.previewImage(arr, current);
  }
})